#!/usr/bin/python
# encoding: utf-8
import re
from django.conf import settings
from django.contrib.gis.db import models

class CAPVIncidentCapture(models.Model):
    finished = models.BooleanField(default=False)
    datetime = models.DateTimeField(auto_now=True)
    
    class Meta:
        app_label = 'mobile'
    
    @staticmethod
    def get_last_capture():
        return CAPVIncidentCapture.objects.filter(finished=True).order_by('-datetime')[:1][0]
    
    def __unicode__(self):
        return u"CAPVIncidentCapture"

class Carretera(models.Model):
    name = models.CharField(blank=False, max_length=100)
    
    class Meta:
        app_label = 'mobile'
        
    def __unicode__(self):
        return self.name
        
class Municipio(models.Model):
    name = models.CharField(blank=False, max_length=100)
    
    class Meta:
        app_label = 'mobile'
        
    def __unicode__(self):
        return self.name

class CausaIncident(models.Model):
    name = models.CharField(blank=False, max_length=100)
    
    class Meta:
        app_label = 'mobile'
        
    def __unicode__(self):
        return self.name

class TipoIncident(models.Model):
    name = models.CharField(blank=False, max_length=100)
    
    class Meta:
        app_label = 'mobile'
        
    def __unicode__(self):
        return self.name
        
class CAPVIncident(models.Model):
    point = models.PointField(blank=False, null=False)
    
    km = models.CharField(blank=True, max_length=10)
    sentido = models.CharField(blank=True, max_length=100)
    afeccion = models.CharField(blank=True, max_length=100)
    retencion = models.CharField(blank=True, max_length=100)
    
    capture = models.ForeignKey(CAPVIncidentCapture)
    municipio = models.ForeignKey(Municipio)
    tipo = models.ForeignKey(TipoIncident)
    causa = models.ForeignKey(CausaIncident)
    carretera = models.ForeignKey(Carretera)
    
    objects = models.GeoManager()
    
    def get_razon(self):
        if self.tipo.name == 'Otras incidencias':
            return self.causa.name
        else:
            return self.tipo.name
        
    def set_data(self, latitud, longitud, idioma, tipologia, causa, cod_car, pk_ini, sentido, fecha, municipio, afeccion, retencion, tipo_imagen, emisor=None):
        #coords
        self.point = u"POINT (%s %s)" % (latitud, longitud)
        
        #tipologia
        tipologia, created = TipoIncident.objects.get_or_create(name=tipologia)
        if created:
            tipologia.save()
        self.tipo = tipologia
        
        #causa
        causa, created = CausaIncident.objects.get_or_create(name=causa)
        if created:
            causa.save()
        self.causa = causa
        
        #carretera
        carretera, created = Carretera.objects.get_or_create(name=cod_car)
        if created:
            carretera.save()
        self.carretera = carretera
        
        #km
        self.km = pk_ini
        
        #km
        self.sentido = sentido.title()
        
        #municipio
        municipio, created = Municipio.objects.get_or_create(name=municipio)
        if created:
            municipio.save()
        self.municipio = municipio
        
        #afeccion
        self.afeccion = afeccion.title()
        
        #retencion
        self.retencion = retencion
        
    class Meta:
        app_label = 'mobile'
        
    def __unicode__(self):
        return u"%s %s %s" % (self.causa, self.municipio, self.carretera)